﻿using System.Threading;

namespace FlitBit.Cache
{
	/// <summary>
	///   Cache stats collector that keeps a count.
	/// </summary>
	public sealed class CountingActiveCacheStatsCollector : CountingCacheStatsCollector, IActiveCacheStatsCollector
	{
		int _resolves;

		/// <summary>
		///   Creates a new instance.
		/// </summary>
		/// <param name="name"></param>
		public CountingActiveCacheStatsCollector(string name)
			: base(name)
		{}

		#region IActiveCacheStatsCollector Members

		/// <summary>
		///   Increments then number of resolves.
		/// </summary>
		/// <param name="count">increment</param>
		public void IncrementResolves(int count)
		{
			Interlocked.Add(ref _resolves, count);
		}

		/// <summary>
		///   Samples the stats.
		/// </summary>
		/// <returns></returns>
		public ActiveCacheStats ActiveSample()
		{
			return new ActiveCacheStats(Sample(), Thread.VolatileRead(ref _resolves));
		}

		#endregion
	}
}